from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadData

from meiduo import settings


def generic_verify_url(userid, email):
    serializer = Serializer(settings.SECRET_KEY, 3600)

    data = {
        "id": userid,
        "email": email
    }

    # 加密
    token = serializer.dumps(data)  # 二进制数据格式

    return 'http://www.meiduo.site:8080/success_verify_email.html?token=' + token.decode()


def check_verify_email_token(token):
    serializer = Serializer(settings.SECRET_KEY, 3600)

    # 加载token
    try:
        token = serializer.loads(token)
    except BadData:
        return None
    else:
        user_id = token.get('id')
        email = token.get('email')

    return user_id
